package com.kzd.gtgame.main.home

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setEmptyView
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.custom.setTransitionStatusT
import com.kzd.gtgame.entity.GameBargain
import com.kzd.gtgame.entity.GameBean
import com.kzd.gtgame.jetpack.vm.TravelViewModel
import com.kzd.gtgame.main.checkLogin
import com.kzd.gtgame.main.game.adapter.Game1Adapter
import com.kzd.gtgame.main.home.adapter.TravelReserveAdapter
import com.kzd.gtgame.main.home.adapter.TravelTodayAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.include_app_refresh.*

class HomeTravelFragment : OwnerViewModeFragment<TravelViewModel>() {

    companion object {
        const val KEY_TRAVEL_TYPE = "KEY_TRAVEL_TYPE"
        fun newInstance(tab: HomeTravelActivity.Tab): HomeTravelFragment {
            val bundle: Bundle = Bundle().apply {
                putSerializable(KEY_TRAVEL_TYPE, tab)
            }
            return HomeTravelFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mTravelTab: HomeTravelActivity.Tab? by lazy {
        arguments?.getSerializable(KEY_TRAVEL_TYPE) as? HomeTravelActivity.Tab
    }

    private val mTravelTodayAdapter = TravelTodayAdapter()

    private val mGame1Adapter = Game1Adapter<GameBean>(
        Game1Adapter.Type.GAME_LIST_RANKING_1
    )

    private val mTravelReserveAdapter = TravelReserveAdapter()

    override fun viewModel(): TravelViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.include_app_refresh

    override fun initView() {
        mTravelTab?.let {
            val adapter = when (it.type) {
                HomeTravelActivity.Tab.Type.TODAY -> mTravelTodayAdapter
                HomeTravelActivity.Tab.Type.WEEK -> mGame1Adapter
                HomeTravelActivity.Tab.Type.RESERVE -> mTravelReserveAdapter
            }
            rvAppRefreshBody.apply {
                layoutManager = LinearLayoutManager(context)
                itemAnimator = null
            }.adapter = adapter
        }

        mTravelReserveAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mTravelReserveAdapter.getItem(position)
            val gid = itemData.id
                ?: return@setOnItemChildClickListener
            val type = if (itemData.is_yuyue)
                "1" else "0"
            if (view.id == R.id.tvTravelReserveSubmit) checkLogin {
                viewModel.reserveChange(gid, type, position)
            }
        }


        mTravelTab?.let {
            when (it.type) {
                HomeTravelActivity.Tab.Type.TODAY -> {
                    mSmartRefreshLayout.setEnableLoadMore(true)
                }
                HomeTravelActivity.Tab.Type.WEEK -> {
                    mSmartRefreshLayout.setEnableLoadMore(false)
                }
                HomeTravelActivity.Tab.Type.RESERVE -> {
                    mSmartRefreshLayout.setEnableLoadMore(true)
                }
            }
        }

        mSmartRefreshLayout.setOnRefreshListener {
            initData()
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            initData(isLoadMore = true)
        }
    }

    override fun initData() {
        initData(isLoadMore = false)
    }

    private fun initData(isLoadMore: Boolean = false) {
        mTravelTab?.let {
            when (it.type) {
                HomeTravelActivity.Tab.Type.TODAY -> {
                    viewModel.travelToday(isLoadMore)
                }
                HomeTravelActivity.Tab.Type.WEEK -> {
                    viewModel.travelWeek()
                }
                HomeTravelActivity.Tab.Type.RESERVE -> {
                    viewModel.travelReserve(isLoadMore)
                }
            }
        }
    }

    override fun initViewMode() {
        viewModel.travelTodayLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusT(response, mTravelTodayAdapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.list
            if (data.isNullOrEmpty()) {
                mTravelTodayAdapter.setEmptyView()
                return@observe
            }
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mTravelTodayAdapter.addData(data)
            } else {
                mTravelTodayAdapter.setNewInstance(data)
            }
        })

        viewModel.travelWeekLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mGame1Adapter, mSmartRefreshLayout, (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mGame1Adapter.setNewInstance(data)
        })

        viewModel.travelReserveLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusT(response, mTravelReserveAdapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.list
            if (data.isNullOrEmpty()) {
                mTravelReserveAdapter.setEmptyView()
                return@observe
            }
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mTravelReserveAdapter.addData(data)
            } else {
                mTravelReserveAdapter.setNewInstance(data)
            }
        })

        viewModel.reserveChangeLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val position = response.data?.position
                ?: return@observe
            val data = response.data?.data
                ?: return@observe
            mTravelReserveAdapter.setData(position, data)
        })
    }

}